package com.xhji.spider.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhji.web.mvc.validation.rules.Create;
import com.xhji.web.mvc.validation.rules.Update;
import com.xhji.common.vo.Page;
import com.xhji.spider.entity.User;
import com.xhji.spider.mapper.UserMapper;
import com.xhji.web.mvc.annotations.LogFilter;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserMapper userMapper;

    @LogFilter(args = true, result = true, headers = true, parameters = true)
    @GetMapping
    public PageInfo<User> page(User user, Page page){
        return PageHelper.startPage(page).doSelectPageInfo(() -> userMapper.select(user));
    }

    @PostMapping
    @Validated(Create.class)
    public void create(@Valid @RequestBody User user){
        userMapper.insert(user);
    }
    @PutMapping
    @Validated(Update.class)
    public void update(@Valid @RequestBody User user){
        userMapper.updateByPrimaryKeySelective(user);
    }
    @DeleteMapping
    public void delete(@RequestParam int id){
        userMapper.deleteByPrimaryKey(id);
    }

}
